/**
 * 项目名：	Rumba Plan
 * 
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2007，所有权利保留。
 * 
 * 文件名：	RB_v20r05.java
 * 模块说明：	
 * 修改历史：
 * Sep 29, 2007 - zhangyanbo - 创建。
 */
package com.hd123.rumba.upgrade.v20.r06;

import java.util.List;

import net.sf.hibernate.Hibernate;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.type.Type;

import org.apache.log4j.Logger;

import com.hd123.rumba.action.user.ViewRoleAdvSys;
import com.hd123.rumba.action.user.ViewRoleSystem;
import com.hd123.rumba.cartridge.CartRumba;
import com.hd123.rumba.domain.IDomain;
import com.hd123.rumba.domain.IDomainMgr;
import com.hd123.rumba.exception.BusinessException;
import com.hd123.rumba.runtime.HibernateSession;
import com.hd123.rumba.runtime.NamingMgr;
import com.hd123.rumba.upgrade.DBCommand;
import com.hd123.rumba.upgrade.Upgrade;
import com.hd123.rumba.user.CRole;
import com.hd123.rumba.user.IPermissionMgr;
import com.hd123.rumba.user.IRole;
import com.hd123.rumba.user.IRoleDomain;
import com.hd123.rumba.user.IRoleMgr;
import com.hd123.rumba.user.RoleDomain;
import com.hd123.rumba.util.Version;

/**
 * 2.0.5.0->2.0.6.0
 * 
 * @author zhangyanbo
 * 
 */
public class RB_v20r05 extends Upgrade {

  public RB_v20r05() throws Exception {
    super();
    baseVersion = new Version("2.0.5.0");
    targetVersion = new Version("2.0.6.0");
    cartUuid = this.getCart(CartRumba.class).getUuid();
    logger = Logger.getLogger(this.getClass());
  }

  @Override
  public void executeDdl() throws BusinessException {
    DBCommand cmd = new DBCommand();
    execFile(cmd, "upgrade.sql");
  }

  @Override
  public void executeData() throws BusinessException {
    logger.info("升级角色");
    try {
      updateRoles();
    } catch (HibernateException e) {
      throw new BusinessException(e);
    }

    logger.info("升级权限");
    try {
      initPermissions();
    } catch (HibernateException e) {
      throw new BusinessException(e);
    }
  }

  private void initPermissions() throws HibernateException, BusinessException {
    IPermissionMgr permMgr = (IPermissionMgr) NamingMgr.getInstance().lookupMgr(
        IPermissionMgr.class);
    IRoleMgr roleMgr = (IRoleMgr) NamingMgr.getInstance().lookupMgr(IRoleMgr.class);
    try {
      permMgr.initPermissionsT(ViewRoleSystem.class);
      permMgr.initPermissionsT(ViewRoleAdvSys.class);

      // 为根域的admin角色添加该权限
      IDomainMgr domainMgr = (IDomainMgr) NamingMgr.getInstance().lookupMgr(IDomainMgr.class);
      IDomain root = domainMgr.getRootDomain();
      IRole admin = roleMgr.getRoleByCode(root, CRole.VALUE_CODE_ROOT_ADMIN);
      permMgr.addPermissionsForRoleT(admin, ViewRoleSystem.class);
      permMgr.addPermissionsForRoleT(admin, ViewRoleAdvSys.class);
    } catch (Exception e) {
      if (e instanceof HibernateException)
        throw (HibernateException) e;
      else
        throw new BusinessException(e);
    }
  }

  private void updateRoles() throws HibernateException, BusinessException {
    Session sess = HibernateSession.getSession();
    Transaction tx = sess.beginTransaction();
    try {
      List list = sess.find("from " + IRole.class.getName() + " o order by o.code");
      for (int i = 0; i < list.size(); i++) {
        IRole role = (IRole) list.get(i);
        List list2 = sess.find("from " + IRoleDomain.class.getName()
            + " rd where rd.role = ? and rd.domain = ?", new Object[] {
            role.getUuid(), role.getDomain().getUuid() }, new Type[] {
            Hibernate.STRING, Hibernate.STRING });
        if (list2.size() == 0) {
          IRoleDomain rd = new RoleDomain();
          rd.setRole(role);
          rd.setDomain(role.getDomain());
          sess.save(rd);
        }
      }
      tx.commit();
    } catch (Exception e) {
      tx.rollback();
      throw new BusinessException(e);
    } finally {
      sess.flush();
    }
  }

}
